package com.service.book;

import com.dao.book.BookDaoImpl;
import com.enitiy.Book;
import com.enitiy.User;
import com.utils.page.Page;

import java.sql.SQLException;
import java.util.List;

/**
 * 创建作者 ：MySunshine
 * 创建时间 2022/10/28 9:05
 * 项目名称 SHUSHAN
 */
public class BookServiceImpl implements BookService {
    BookDaoImpl bdi = new BookDaoImpl();

    @Override //全部查询
    public List<Book> SelectBookAll(Page p) {
        int userAllNum = 0;
        try {
            userAllNum = bdi.BookSelectAllNum(p);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        p.setTotalCounts(userAllNum);
        System.out.println("总条数" + userAllNum);
        int totalPage = userAllNum % p.getPageSize() == 0 ? userAllNum / p.getPageSize() : userAllNum / p.getPageSize() + 1;
        p.setTotalPages(totalPage);
        System.out.println("总页数" + totalPage);
        List<Book> books = null;
        try {
            books = bdi.SelectBookAll(p);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return books;
    }

    public List<Book> SelectBookAll(){
        List<Book> books = null;
        try {
            books = bdi.SelectBookAll();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return books;

    }
    @Override//根据id删除book
    public boolean DeleteBookById(int i) {
        boolean b = false;
        try {
            b = bdi.DeleteBookById(i);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return b;
    }

    @Override//根据id查询book
    public Book SelectBookById(int i) {
        Book book = null;
        try {
            book = bdi.SelectBookById(i);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return book;
    }

    @Override//根据id修改book
    public boolean UpdateBookById(Book b) {
        boolean b1 = false;
        try {
            b1 = bdi.UpdateBookById(b);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return b1;
    }

    @Override//添加书籍
    public boolean AddBook(Book book) {
        boolean b = false;
        try {
            b = bdi.AddBook(book);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return b;
    }

    @Override //根据类型查询书籍
    public List<Book> SelectBookByKid(int k) {
        List<Book> books = null;
        try {
            books = bdi.SelectBookByKid(k);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return books;
    }
}
